﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;

namespace FYPHP
{
    public partial class UpdateForAdmin : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    SqlCommand cmdUser;
                    SqlDataReader dtrUser;
                    conn.Open();

                    cmdUser = new SqlCommand("Select * From Users where u_id = '1'", conn);
                    dtrUser = cmdUser.ExecuteReader();
                    dtrUser.Read();

                    ID = dtrUser["u_id"].ToString();
                    String userName = dtrUser["username"].ToString();
                    String password = dtrUser["password"].ToString();
                    String IC = dtrUser["u_ic"].ToString();
                    String age = dtrUser["u_age"].ToString();
                    String gender = dtrUser["u_gender"].ToString();
                    String address = dtrUser["u_address"].ToString();
                    String phone = dtrUser["u_phone"].ToString();
                    String email = dtrUser["u_email"].ToString();
                   


                    lblName.Text = userName;
                    txtIC.Text = IC;
                    txtAge.Text = age;
                    rblGender.SelectedValue = gender;
                    txtAdd1.Text = address;
                    txtPhone.Text = phone;
                    txtEmail.Text = email;
                    

                    dtrUser.Close();
                    conn.Close();
                }
                catch (SqlException ex)
                {

                }
            }


        }


        public String return_userpicture()
        {
            SqlCommand cmdUser;
            SqlDataReader dtrUser;
            conn.Open();

            cmdUser = new SqlCommand("Select * From Users", conn);
            dtrUser = cmdUser.ExecuteReader();
            dtrUser.Read();
            String userPicture = dtrUser["u_picture"].ToString();


            dtrUser.Close();
            conn.Close();

            return userPicture;

        }

        protected void UploadButton_Click(object sender, EventArgs e)
        {
            String saveDir = @"\uploads\products\";
            string appPath = Request.PhysicalApplicationPath;

            if (FileUpload1.HasFile)
            {
                string savePath = appPath + saveDir + Server.HtmlEncode(FileUpload1.FileName);
                String fileName = FileUpload1.FileName;

                FileUpload1.SaveAs(savePath);
                try
                {
                    conn.Open();

                    SqlCommand cmdUpdate = new SqlCommand("Update Users SET u_picture=@Picture where u_id =@Id ", conn);

                    cmdUpdate.Parameters.AddWithValue("@Picture", imgName.Text);
                    cmdUpdate.Parameters.AddWithValue("@Id", 1);

                    int x = cmdUpdate.ExecuteNonQuery();
                    conn.Close();
           
                }
                catch (SqlException ex)
                {

                }


                UploadStatusLabel.Text = "Your file was saved as ";
                imgName.Text = fileName;
            }
            else
            {
                UploadStatusLabel.Text = "You did not specify a file to upload.";
                imgName.Text = "";
            }
        }


        protected void UpdateMyProfile_Click(object sender, EventArgs e)
        {

            try
            {
                String IC = txtIC.Text;
                int Age = Convert.ToInt32(txtAge.Text);
                String Gender = rblGender.SelectedValue;
                String fullAddress = txtAdd1.Text + "|" + txtAdd2.Text + "|" + txtPostCode.Text + "|" + txtCity.Text + "|" + txtState.Text + "|" + txtCountry.Text;
                String Email = txtEmail.Text;
                String PhoneNo = txtPhone.Text;

                conn.Open();

                SqlCommand cmdUpdate = new SqlCommand("Update Users SET u_ic=@Ic,u_age=@Age,u_gender=@Gender,u_address=@Address,u_email=@Email,u_phone=@Phone,u_picture=@Picture where u_id =@Id ", conn);
                //SqlCommand cmdUpdate = new SqlCommand("Update Users SET u_ic=@Ic where u_id =@Id ", conn);

                cmdUpdate.Parameters.AddWithValue("@Ic", IC);
                cmdUpdate.Parameters.AddWithValue("@Age", Age);
                cmdUpdate.Parameters.AddWithValue("@Gender", Gender);
                cmdUpdate.Parameters.AddWithValue("@Address", fullAddress);
                cmdUpdate.Parameters.AddWithValue("@Phone", PhoneNo);
                cmdUpdate.Parameters.AddWithValue("@Email", Email);
                cmdUpdate.Parameters.AddWithValue("@Picture", imgName.Text);
                cmdUpdate.Parameters.AddWithValue("@Id", 1);

                int x = cmdUpdate.ExecuteNonQuery();

                Response.Write(x + "_xxx" + IC);

                conn.Close();
            }
            catch (SqlException ex)
            {

            }

        }
    }
}